//
//  EredarViewController.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/23.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "EredarViewController.h"
#import "EredarModel.h"
#import "EredarTableViewCell.h"
#import "UserDetailViewController.h"
@interface EredarViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSInteger _page;
}
@property (nonatomic, retain)UITableView *eredarTableview;//达人推荐tableview
@property (nonatomic, retain) NSMutableArray *dataArr;//达人推荐数组
@property (nonatomic, retain) NSMutableArray *dataarr;//图片数组
@end

@implementation EredarViewController

-(void)dealloc
{
    [_eredarTableview release];
    [_dataArr release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    [self data];
    
    #pragma mark - 创建tableview
    
    self.eredarTableview = [[UITableView alloc]initWithFrame:self.view.frame];
    //代理
    self.eredarTableview.dataSource = self;
    self.eredarTableview.delegate = self;
    //添加
    [self.view addSubview:self.eredarTableview];
    //重用池
    [_eredarTableview registerClass:[EredarTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_eredarTableview release];
    //刷新
    [self creatUI];
}
#pragma mark - tableview必须实现的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EredarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    EredarModel *eredar = _dataArr[indexPath.row];
    [cell.touxiang sd_setImageWithURL:[NSURL URLWithString:eredar.avatar]];//头像
    cell.name.text = eredar.nick_name;//名字
    cell.courseAndhandWork.text = [[[eredar.course_count stringByAppendingString:@"个教程  |  "] stringByAppendingString:eredar.opus_count] stringByAppendingString:@"个手工圈"];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
#pragma mark - 数据请求
- (void)data
{
    NSString *str = @"http://m.shougongke.com/index.php?&c=Index&a=daren";
    [AFNnetWork GETwithURL:str completion:^(id result) {
        NSArray *arr = result[@"data"];
        self.dataArr = [EredarModel modelHandlerWithArray:arr];
        
        
//        self.dataarr = [NSMutableArray array];
//        for (NSDictionary *dic in arr) {
//            NSArray *iArr = [ dic objectForKey:@"list"];
//            EredarModel *ere =[[ EredarModel alloc]init];
//            ere.imageArr = [NSMutableArray array];
//            for (NSDictionary *dicarr in iArr) {
//                [ere.imageArr addObject:[dicarr objectForKey:@"host_pic"]];
//            }
//            [self.dataarr addObject:ere];
//            
//            NSLog(@"%@",ere.imageArr);
//        }
        [_eredarTableview reloadData];
        [self.eredarTableview.mj_header endRefreshing];
    }];
}
#pragma mark - 刷新 
-(void)creatUI
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self data];
    }];
    [header setTitle:@"我刷我刷我刷刷刷" forState:MJRefreshStatePulling];
    [header beginRefreshing];//开始刷新
    self.eredarTableview.mj_header = header;//放在头部上

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UserDetailViewController *userdetail = [[UserDetailViewController alloc]init];
    EredarModel *e = _dataArr[indexPath.row];
    userdetail.uid = e.user_id;
    userdetail.faceurl =e.avatar;
    [self.navigationController pushViewController:userdetail animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
